package demo1;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author liangzai
 * @Description:
 */
public class Training {
    //距离相等的条形码
    public int[] rearrangeBarcodes(int[] barcodes) {
        int n = barcodes.length;
        HashMap<Integer,Integer> map = new HashMap<>();
        int max = 0;
        int maxKey = 0;
        for (int i = 0; i < n; i++) {
            map.put(barcodes[i],map.getOrDefault(barcodes[i],0)+1);
            if(max<map.get(barcodes[i])){
                max = map.get(barcodes[i]);
                maxKey = barcodes[i];
            }
        }
        int index = 0;
        int[] ret = new int[n];
        for (int i = 0; i < max; i++) {
            ret[index] = maxKey;
            index+=2;
        }
        map.put(maxKey,0);
        for(Map.Entry<Integer,Integer> entrySet: map.entrySet()){
            for (int i = 0; i < entrySet.getValue(); i++) {
                if(index>=n) index = 1;
                ret[index] = entrySet.getKey();
                index+=2;
            }
        }
        return ret;
    }

    //重构字符串
    public String reorganizeString(String s) {
        int len = s.length();
        char[] ch = s.toCharArray();
        int[] hash = new int[26];
        char maxCharacter = 'a';
        int max = 0;
        for (int i = 0; i < len; i++) {
            if(max<++hash[ch[i]-'a']){
                max = hash[ch[i]-'a'];
                maxCharacter = ch[i];
            }
        }
        if(max>(len+1)/2) return "";
        int index = 0;
        char[] ret = new char[len];
        for (int i = 0; i < max; i++) {
            ret[index] = maxCharacter;
            index+=2;
        }
        hash[maxCharacter-'a'] = 0;
        for (int i = 0; i < 26; i++) {
            for (int j = 0; j < hash[i]; j++) {
                if(index>=len) index=1;
                ret[index] = (char)(i+'a');
                index+=2;
            }
        }
        return new String(ret);
    }

    //整数替换
    Map<Long,Integer> map;
    public int integerReplacement(int n) {
        map = new HashMap<>();
        return dfs(n*1l);
    }

    private int dfs(long n){
        if(map.containsKey(n)) return map.get(n);
        if(n==1) {
            map.put(n,0);
            return 0;
        }
        if(n%2==0){
            map.put(n,dfs(n/2)+1);
            return map.get(n);
        }else {
            map.put(n,Math.min(dfs(n+1),dfs(n-1))+1);
            return map.get(n);
        }
    }

    public int integerReplacement1(int n) {
        int count = 0;
        int tmp = n;
        while (tmp > 1) {
            if (tmp == 3) {
                count+=2;
                break;
            } else if (tmp % 2 == 0) {
                tmp /= 2;
                count++;
            } else {
                if (tmp % 4 == 1) {
                    tmp -= 1;
                    count++;
                } else {
                    tmp++;
                    count++;
                }
            }
        }
        return count;
    }

}
